﻿using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using EmbeddedLinkUpdater.Code.Documents;

namespace EmbeddedLinkUpdater.Code
{
    public abstract class DocumentFactory
    {
        public static Document GetDocumentType(string fileName)
        {
            var fileType = new FileInfo(fileName);
            
            if(Regex.IsMatch(fileType.Extension, @"\.(?:" + ConfigSettings.WordFileExtensions + ")$"))
                return new WordDocument(fileName);

            if (Regex.IsMatch(fileType.Extension, @"\.(?:" + ConfigSettings.ExcelFileExtensions + ")$"))
                return new ExcelDocument(fileName);

            if (Regex.IsMatch(fileType.Extension, @"\.(?:" + ConfigSettings.PowerPointFileExtensions + ")$"))
                return new PowerPointDocument(fileName);

            return null;
        }

        public static string GetSupportedDocumentTypes(string documentTypes)
        {
            var result = string.Empty;
            var splitString = documentTypes.Split('|');
            return splitString.Aggregate(result, (current, key) => current + ConfigSettings.GetSettingValueByKey(key));
        }
    }
}
